package com.sudichina.sudichina.view.activity.setting;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.common.reflect.TypeToken;
import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.utils.DESMD5Utils;
import com.sudichina.sudichina.utils.Toastutils;
import com.sudichina.sudichina.utils.Util;
import com.sudichina.sudichina.view.activity.main.BaseActivity;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangeDealPswActivity extends BaseActivity implements onRequestDataFinish {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.et_acdp_orign_newpsw)
    EditText etAcdpOrignNewpsw;
    @BindView(R.id.et_acdp_newpsw)
    EditText etAcdpNewpsw;
    @BindView(R.id.et_acdp_confim_newpsw)
    EditText etAcdpConfimNewpsw;
    @BindView(R.id.bt_acdp_confim)
    Button btAcdpConfim;
    @BindView(R.id.activity_change_phone_number)
    LinearLayout activityChangePhoneNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_deal_psw);
        ButterKnife.bind(this);
        tvTitle.setText("修改交易密码");
    }

    @OnClick({R.id.iv_back, R.id.bt_acdp_confim})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_acdp_confim:
                String orignpsw = etAcdpOrignNewpsw.getText().toString();
                String newpsw = etAcdpNewpsw.getText().toString();
                String newpsw2 = etAcdpConfimNewpsw.getText().toString();
                if (!TextUtils.equals(newpsw, newpsw2)) {

                    Toastutils.showToast("两次输入的新密码不一致");

                    return;
                }
                if (TextUtils.equals(orignpsw, newpsw)) {

                    Toastutils.showToast("新密码和旧密码一致\n无须修改");
                    return;

                }

                boolean b = !Util.IsPassword(orignpsw);

                boolean b1 = !Util.IsPassword(newpsw);
                boolean b2 = !Util.IsPassword(newpsw2);
                if (b && b1 && b2) {
                    Toast.makeText(this, "密码长度为6-16位\n必须包含数字和字母", Toast.LENGTH_SHORT).show();
                    return;
                }
                
                confirmtionOld(orignpsw);







                break;
        }
    }

    private void changeTradePwd(String newpsw2) {

        HashMap<String, Object> map = new HashMap<>();
        /*
        * accountName: String 手机号
tradePassword：String 交易密码  MD5加密
        *
        * */
        map.put("accountName", MyApp.getCurrentUser().getAccountName().toString());
        map.put("tradePassword", DESMD5Utils.MD5(newpsw2,false));
        NetUtils.requestData(Api.FORGOT, map, this, new TypeToken<Result>() {
        }.getType(), false);

    }

    private void confirmtionOld(String orignpsw) {


        HashMap<String, Object> map = new HashMap<>();
        /*
        * accountName: String 手机号
tradePassword：String 交易密码  MD5加密
        *
        * */
        map.put("accountName", MyApp.getCurrentUser().getAccountName().toString());
        map.put("tradePassword", DESMD5Utils.MD5(orignpsw,false));
        NetUtils.requestData(Api.ISTRUE_TRADEPASSWORD_DEAL_PSW, map, this, new TypeToken<Result>() {
        }.getType(), false);
    }

    @Override
    public void requestdataFinish(String catalog, Object bean) {
        switch (catalog) {
            case Api.ISTRUE_TRADEPASSWORD_DEAL_PSW:
                Result istruebean = (Result) bean;
                switch (istruebean.getCode()) {
                    case 8004:
  Toastutils.showToast("原始密码输入错误");
                        return;
                    case 1:
                        changeTradePwd(etAcdpNewpsw.getText().toString());
                        return;
                }
                break;
            case Api.FORGOT:
                Result chargeTradePwdBean = (Result) bean;
                switch (chargeTradePwdBean.getCode()) {
                    case 8009:
                        Toast.makeText(this, "密码重置失败\n请稍后重试", Toast.LENGTH_SHORT).show();
                        return;
                    case 1:
                        Toast.makeText(this, "密码重置成功\n请牢记你的交易密码", Toast.LENGTH_SHORT).show();
                        MyApp.getCurrentUser().setTradePassword(DESMD5Utils.MD5(etAcdpNewpsw.getText().toString(), false));
                        finish();
                        return;
                }
                break;
        }

    }

    @Override
    public void requestdataFailed(IOException e) {
        Toastutils.showToast("IOException:" + e);

    }
}
